package cn.com.git.admin.config.security;

import cn.com.git.admin.service.dto.AuthenticationDTO;
import cn.com.git.admin.service.dto.AuthenticationTokenDTO;
import org.springframework.stereotype.Component;

/**
 * 当前登录用户池
 * @author zhangzhenfu
 * @Description
 * @date 2020/3/26 10:24
 */
@Component
public class LoginContext {

    private  ThreadLocal<AuthenticationTokenDTO> userThreadLocal = new ThreadLocal<AuthenticationTokenDTO>();


    public AuthenticationTokenDTO getCurrentUser() {
        return userThreadLocal.get();
    }

    public void setCurrentUser(AuthenticationTokenDTO currentUser) {
        userThreadLocal.set(currentUser);
    }

    public  Integer getLoginUserId() {
        if(userThreadLocal.get()==null){
            return null;
        }
        return userThreadLocal.get().getSysUser().getId();
    }

    @Override
    protected void finalize() throws Throwable {
        userThreadLocal.set(null);
        super.finalize();
    }

}
